package com.merlin.meta.domain.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.merlin.framework.core.bean.PageBean;
import com.merlin.framework.core.bean.Result;
import com.merlin.framework.core.label.InterfaceApiLabel;
import com.merlin.framework.core.lib.handler.ResultPageBeanUtils;
import com.merlin.meta.domain.service.MetaDomainService;
import com.merlin.meta.domain.vo.MetaDomainAddVo;
import com.merlin.meta.domain.vo.MetaDomainUpdateVo;
import com.merlin.meta.domain.vo.MetaDomainVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
* @ClassName:：MetaDomainController 
* @Description： 元数据领域资源事件层
* @author ：张海滨(zhanghbs)
* @date ：2019年11月12日 下午8:23:56 
*
 */
@RestController
@RequestMapping("/meta")
@Api(value="MetaDomainController",description="元数据领域资源的API接口")
public class MetaDomainController {
      
	@Autowired
	private MetaDomainService metaDomainService;
	    
	/**
	 * 
	* @Title：createMaster 
	* @Description：领域资源创建
	* @param ：@param trainMasterAddVo
	* @param ：@param bindingResult
	* @param ：@param headers
	* @param ：@return 
	* @return ：Result<Integer> 
	* @throws
	 */
	@ApiOperation(value = "domain_meta_domain_0001-领域资源创建", notes = "领域资源创建")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "Authorization", value = "新式令牌", required = false, dataType = "String", paramType = "header"),
		@ApiImplicitParam(name = "Merlin_BUSINESS_SERIALID", value = "交易流水", required = false, dataType = "String", paramType = "header",defaultValue="35535cc7-5037-11e7-b2ef-5254f7f2fbdd"),
		@ApiImplicitParam(name = "Merlin_REQUEST_SYS_CODE", value = "请求方系统编码", required = false, dataType = "String", paramType = "header",defaultValue="swagger"),
		@ApiImplicitParam(name = "Merlin_REQUEST_MODULE_CODE", value = "请求方模块编码", required = false, dataType = "String", paramType = "header",defaultValue="swagger"),
		@ApiImplicitParam(name = "Merlin_REQUEST_NODE_IP", value = "请求方节点IP", required = false, dataType = "String", paramType = "header",defaultValue="219.142.80.211"),
		@ApiImplicitParam(name = "Accept", value = "接收属性", required = true,dataType="String",paramType="header",defaultValue="application/json"),
		@ApiImplicitParam(name = "Accept-Charset", value = "接收字符集", required = true,dataType="String",paramType="header",defaultValue="utf-8"),
		@ApiImplicitParam(name = "Content-Type", value = "内容类型", required = true,dataType="String",paramType="header",defaultValue="application/json; charset=UTF-8")
	})
	@RequestMapping(value="/master",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
	@InterfaceApiLabel(value = "domain_meta_domain_0001",description="领域资源创建")
	public Result<Integer> createMaster(@Valid @RequestBody MetaDomainAddVo trainMasterAddVo, BindingResult bindingResult, @RequestHeader HttpHeaders headers){		
		//新增
		int code = metaDomainService.insertSelective(trainMasterAddVo);
		return new Result<Integer>("0","新增领域成功",code);
	}
      /**
       * 
      * @Title：getDomain 
      * @Description：领域资源单记录查询
      * @param ：@param code
      * @param ：@param headers
      * @param ：@return 
      * @return ：Result<MetDomainVo> 
      * @throws
       */
	@ApiOperation(value = "domain_meta_domain_0002-领域资源单记录查询", notes = "领域资源单记录信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "code", value = "编码", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "Authorization", value = "新式令牌", required = false, dataType = "String", paramType = "header"),
			@ApiImplicitParam(name = "Merlin_BUSINESS_SERIALID", value = "交易流水", required = false, dataType = "String", paramType = "header",defaultValue="35535cc7-5037-11e7-b2ef-5254f7f2fbdd"),
			@ApiImplicitParam(name = "Merlin_REQUEST_SYS_CODE", value = "请求方系统编码", required = false, dataType = "String", paramType = "header",defaultValue="swagger"),
			@ApiImplicitParam(name = "Merlin_REQUEST_MODULE_CODE", value = "请求方模块编码", required = false, dataType = "String", paramType = "header",defaultValue="swagger"),
			@ApiImplicitParam(name = "Merlin_REQUEST_NODE_IP", value = "请求方节点IP", required = false, dataType = "String", paramType = "header",defaultValue="219.142.80.211"),
			@ApiImplicitParam(name = "Accept", value = "接收属性", required = true,dataType="String",paramType="header",defaultValue="application/json"),
			@ApiImplicitParam(name = "Accept-Charset", value = "接收字符集", required = true,dataType="String",paramType="header",defaultValue="utf-8"),
			@ApiImplicitParam(name = "Content-Type", value = "内容类型", required = true,dataType="String",paramType="header",defaultValue="application/json; charset=UTF-8")
	})
	@RequestMapping(value = "/domain/{code}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@InterfaceApiLabel(value = "domain_meta_domain_0002", description = "领域资源单记录查询")
	public Result<MetaDomainVo> getDomain(@PathVariable String code,@RequestHeader HttpHeaders headers) {
		MetaDomainVo metaDomainVo = metaDomainService.selectByPrimaryKey(code);
		metaDomainVo=JSON.parseObject(JSON.toJSONString(metaDomainVo), MetaDomainVo.class);
		 return new Result<MetaDomainVo>("0", metaDomainVo);
	}
   /**
    * 
   * @Title：searchDomains 
   * @Description：领域资源列表查询
   * @param ：@param code
   * @param ：@param name
   * @param ：@param pageBean
   * @param ：@param headers
   * @param ：@return 
   * @return ：Result<List<MetDomainVo>> 
   * @throws
    */
	@ApiOperation(value = "domain_meta_domain_0003-领域资源列表查询", notes = "查询领域资源列表信息") 
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "code", value = "编码", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "number", value = "档期查询页", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "size", value = "每页记录数", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "Authorization", value = "新式令牌", required = false, dataType = "String", paramType = "header"),
			@ApiImplicitParam(name = "Merlin_BUSINESS_SERIALID", value = "交易流水", required = false, dataType = "String", paramType = "header",defaultValue="35535cc7-5037-11e7-b2ef-5254f7f2fbdd"),
			@ApiImplicitParam(name = "Merlin_REQUEST_SYS_CODE", value = "请求方系统编码", required = false, dataType = "String", paramType = "header",defaultValue="swagger"),
			@ApiImplicitParam(name = "Merlin_REQUEST_MODULE_CODE", value = "请求方模块编码", required = false, dataType = "String", paramType = "header",defaultValue="swagger"),
			@ApiImplicitParam(name = "Merlin_REQUEST_NODE_IP", value = "请求方节点IP", required = false, dataType = "String", paramType = "header",defaultValue="219.142.80.211"),
			@ApiImplicitParam(name = "Accept", value = "接收属性", required = true,dataType="String",paramType="header",defaultValue="application/json"),
			@ApiImplicitParam(name = "Accept-Charset", value = "接收字符集", required = true,dataType="String",paramType="header",defaultValue="utf-8"),
			@ApiImplicitParam(name = "Content-Type", value = "内容类型", required = true,dataType="String",paramType="header",defaultValue="application/json; charset=UTF-8")
	})
	@RequestMapping(value = "/domains", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@InterfaceApiLabel(value = "domain_meta_domain_0003", description = "查询领域资源列表信息")
	public Result<List<MetaDomainVo>> searchDomains(String code,String name, PageBean pageBean, @RequestHeader HttpHeaders headers) {
		MetaDomainVo metaDomainVo=new MetaDomainVo();
		metaDomainVo.setCode(code);
		metaDomainVo.setName(name);
		List<MetaDomainVo> metDomainVoList = metaDomainService.search(metaDomainVo, pageBean);
		
		List<MetaDomainVo> metDomainVoResultList=new ArrayList<MetaDomainVo>();;

			for(MetaDomainVo  metDomainVo1 : metDomainVoResultList)
			{
				MetaDomainVo metDomainVo2=JSON.parseObject(JSON.toJSONString(metDomainVo1), MetaDomainVo.class);
				metDomainVoResultList.add(metDomainVo2);
			}
			Result<List<MetaDomainVo>> result = new Result<List<MetaDomainVo>>("0",metDomainVoResultList);

			ResultPageBeanUtils.pageResult(result, pageBean, "metaDomainService", "searchCount", metaDomainVo);
		//返回查询结果
		return result;
	}
	/**
	 * 
	* @Title：deleteDomain 
	* @Description：领域资源删除
	* @param ：@param objectCd
	* @param ：@param code
	* @param ：@return 
	* @return ：Result<String> 
	* @throws
	 */
	@ApiOperation(value = "domain_meta_domain_0004-领域资源删除", notes = "领域资源删除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "Authorization", value = "新式令牌", required = false, dataType = "String", paramType = "header"),
			@ApiImplicitParam(name = "TK_BUSINESS_SERIALID", value = "交易流水", required = false, dataType = "String", paramType = "header",defaultValue="35535cc7-5037-11e7-b2ef-5254f7f2fbdd"),
			@ApiImplicitParam(name = "TK_REQUEST_SYS_CODE", value = "请求方系统编码", required = false, dataType = "String", paramType = "header",defaultValue="swagger"),
			@ApiImplicitParam(name = "TK_REQUEST_MODULE_CODE", value = "请求方模块编码", required = false, dataType = "String", paramType = "header",defaultValue="swagger"),
			@ApiImplicitParam(name = "TK_REQUEST_NODE_IP", value = "请求方节点IP", required = false, dataType = "String", paramType = "header",defaultValue="219.142.80.211"),
			@ApiImplicitParam(name = "Accept", value = "接收属性", required = true, dataType = "String", paramType = "header", defaultValue = "application/json"),
			@ApiImplicitParam(name = "Accept-Charset", value = "接收字符集", required = true, dataType = "String", paramType = "header", defaultValue = "utf-8"),
			@ApiImplicitParam(name = "Content-Type", value = "内容类型", required = true, dataType = "String", paramType = "header", defaultValue = "application/json; charset=UTF-8") })
	@RequestMapping(value = "/domain/{code}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	@InterfaceApiLabel(value = "domain_meta_domain_0004", description = "通用主数据删除")
	public Result<String> deleteDomain(@PathVariable String code) {

		int flag = metaDomainService.deleteDomain(code);
		if (flag == 1) {
			return new Result<String>("0");// 返回成功
		} else {
			return new Result<String>("1");// 返回数据不存在
		}

	}
    /**
     * 
    * @Title：alterDomain 
    * @Description：领域资源修改
    * @param ：@param metDomainUpdateVo
    * @param ：@param bindingResult
    * @param ：@return 
    * @return ：Result<String> 
    * @throws
     */
	@ApiOperation(value = "domain_meta_domain_0005-领域资源修改", notes = "领域资源修改")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "Authorization", value = "新式令牌", required = false, dataType = "String", paramType = "header"),
			@ApiImplicitParam(name = "code", value = "编码", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "TK_BUSINESS_SERIALID", value = "交易流水", required = false, dataType = "String", paramType = "header",defaultValue="35535cc7-5037-11e7-b2ef-5254f7f2fbdd"),
			@ApiImplicitParam(name = "TK_REQUEST_SYS_CODE", value = "请求方系统编码", required = false, dataType = "String", paramType = "header",defaultValue="swagger"),
			@ApiImplicitParam(name = "TK_REQUEST_MODULE_CODE", value = "请求方模块编码", required = false, dataType = "String", paramType = "header",defaultValue="swagger"),
			@ApiImplicitParam(name = "TK_REQUEST_NODE_IP", value = "请求方节点IP", required = false, dataType = "String", paramType = "header",defaultValue="219.142.80.211"),
			@ApiImplicitParam(name = "Accept", value = "接收属性", required = true, dataType = "String", paramType = "header", defaultValue = "application/json"),
			@ApiImplicitParam(name = "Accept-Charset", value = "接收字符集", required = true, dataType = "String", paramType = "header", defaultValue = "utf-8"),
			@ApiImplicitParam(name = "Content-Type", value = "内容类型", required = true, dataType = "String", paramType = "header", defaultValue = "application/json; charset=UTF-8") })
	@RequestMapping(value = "/domain/{code}", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	@InterfaceApiLabel(value = "domain_meta_domain_0005", description = "领域资源修改")
	public Result<String> alterDomain(@Valid @RequestBody MetaDomainUpdateVo metaDomainUpdateVo,BindingResult bindingResult) {
		//保存
		int flag = metaDomainService.alterDomain(metaDomainUpdateVo);
        if(flag!=1){
        	return new Result<String>("1");
        }
		return new Result<String>("0");
	}
}
